package net.aiion.weave.impl.resources;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;

import net.aiion.weave.spi.resources.Resource;
import net.aiion.weave.spi.resources.ResourceException;

public class ResourceImpl implements Resource
{
    private final String resourceName;
    private final URL resourceURL;

    public ResourceImpl(final String aResourceName, final URL aResourceURL) {
        resourceName = aResourceName;
        resourceURL = aResourceURL;
    }

    @Override
    public String getName() {
        return resourceName;
    }

    @Override
    public URL getURL() {
        return resourceURL;
    }

    @Override
    public InputStream openStream() {
        if (resourceURL == null) {
            return null;
        }

        try {
            return resourceURL.openStream();
        } catch (final IOException e) {
            throw new ResourceException(e);
        }
    }

    @Override
    public int hashCode() {
        return 31 + Objects.hashCode(resourceURL);
    }

    @Override
    public boolean equals(final Object obj) {
        if (obj == this) {
            return true;
        }

        if (!(obj instanceof ResourceImpl)) {
            return false;
        }

        return Objects.equals(((ResourceImpl) obj).resourceURL, resourceURL);
    }
}
